﻿// <copyright file="AdminSettingsDymanicNodeProvider.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Cms.Services.Infrastructure
{
    using MvcSiteMapProvider;
    using System;
    using System.Linq;
    using System.Collections.Generic;

    using CleverCMS.Core.Infrastructure;

    public class AdminSettingsDymanicNodeProvider : DynamicNodeProviderBase
    {
        private SettingService _settingService;
        private readonly AppCache appCache;

        public AdminSettingsDymanicNodeProvider()
        {
            this.appCache = new AppCache();
        }

        public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)
        {
            if (appCache.Exists("AdminSettings"))
                return (List<DynamicNode>)appCache.Get("AdminSettings");

            _settingService = IoC.Get<SettingService>();
            var nodes = new List<DynamicNode>();

            var settings = _settingService.LoadAll().ToList();
            foreach (var setting in settings)
            {
                var settingNode = new DynamicNode
                {
                    Key = "setting_" + Guid.NewGuid() + "_" + setting.Id,
                    Title = setting.Key
                };
                settingNode.RouteValues.Add("id", setting.Id);
                settingNode.Action = "Edit";
                settingNode.Controller = "Settings";
                settingNode.Area = "CleverCms.Admin";
                nodes.Add(settingNode);
            }

            appCache.Add("AdminSettings", nodes);
            return nodes;
        }
    }
}
